<?php
/**
 * Created by PhpStorm.
 * User:雨寒
 * Date: 2018/9/19
 * Time: 13:39
 */

namespace app\www\controller;

use think\Session;
use think\Db;
use app\common\model\User;
use app\common\model\DeveloperException;
class Resource extends Common
{
    // 活动列表
    public function resourceList(){

        $data=input();
        $page=empty($data['page'])?1:input('page');
        $page_size=empty($data['page_size'])?5:input('page_size');
        $access_token=Session::get("access_token");

        $data1=array(
            "module"=>'Active',
            "interface"=>'1001',
            "version"=>'1.0.0',
            "data"=> [
                "page_size"=> $page_size,
                "page"=> $page,
                "status"=> '-1',
                "access_token"=> $access_token
            ]
        );
        if(request()->isAjax())
        {
            $res=curl_post(config('self_url'),json_encode($data1));
            $result_code=json_decode($res,true)['result_code'];
            $result_info=json_decode($res,true)['result_info'];
            if($result_code!=="0000")
            {
                return 0;
            }else{
                $result_data=json_decode($res,true)['result_data']['list'];
                return json_encode($result_data);
            }
        }else{
            $res=curl_post(config('self_url'),json_encode($data1));
            $result_code=json_decode($res,true)['result_code'];
            $result_info=json_decode($res,true)['result_info'];
            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                return $this->error($result_info);

            }
            $result_data=json_decode($res,true)['result_data'];
            $this->assign("list",$result_data['list']);
            return $this->fetch();
        }

    }

    /*活动详情*/
    public function resourceInfo(){
        $id=input('id');
        $access_token=Session::get("access_token");
        $data1=array(
            "module"=>'Active',
            "interface"=>'1002',
            "version"=>'1.0.0',
            "data"=> [
                "active_id"=> $id,
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            return $this->error($result_info);

        }else{
            $result_info=json_decode($res,true)['result_data'];
            $this->assign("info",$result_info);
            return $this->fetch();
        }

    }

    /*参与活动*/
    public function resourceBuy(){
        $id=input('id');
        $access_token=Session::get("access_token");
        $user=User::userFromToken($access_token);
        $data1=array(
            "module"=>'Active',
            "interface"=>'1002',
            "version"=>'1.0.0',
            "data"=> [
                "active_id"=> $id,
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            return $this->error($result_info);

        }else{
            $result_info=json_decode($res,true)['result_data'];
            $this->assign("info",$result_info);
            $this->assign("user",$user);
            /*查看用户是否开启钱包*/
            $time=time();
            $data2=array(
                "time"=>$time,
                "mobile"=>$user['mobile'],
                "token"=>md5(md5(substr($time,3)))
            );
            $url=config("post_url")."/commerce/usertrue";
            $res= curl_post($url,json_encode($data2));
            $data=json_decode($res,true);

            if($data['error_code']==0)
            {
                 $this->assign('mycoin',$data['result']['hlink']);
            }else{
                $this->error("您还没有开启钱包，请去‘钱包’并设置支付密码！");
            }

            $is_app=input('is_app');
            $this->assign('is_app',$is_app);

            return $this->fetch();
        }

    }

    /*活动支付*/
    public function resourcePay(){
        $access_token=Session::get("access_token");
       if(request()->isAjax())
       {
            $data=input();
           $data1=array(
               "module"=>'Active',
               "interface"=>'1003',
               "version"=>'1.0.0',
               "data"=> [
                   "active_id"=> $data['active_id'],
                   "order_type"=> 2,
                   "pay_type"=> $data['paytype'],
                   "coin"=> $data['mycoin'],
                   "access_token"=> $access_token
               ]
           );
           $res=curl_post(config('self_url'),json_encode($data1));
           $result_code=json_decode($res,true)['result_code'];
           $result_info=json_decode($res,true)['result_info'];
           if($result_code!=="0000")
           {
               $this->ajaxError($result_info);
           }else{
               $result_data=json_decode($res,true)['result_data'];
                $this->ajaxSuccess(json_encode($result_data));
           }
       }
    }

    /*联盟商列表*/
    public function unionList()
    {
        $data=input();
        $page=empty($data['page'])?1:input('page');
        $this->assign('page',$page);
        $page_size=empty($data['page_size'])?5:input('page_size');
        $category_id=empty($data['category_id'])?0:$data['category_id'];
        $this->assign('category_id',$category_id);
        $access_token=Session::get("access_token");
        /*获取分类*/
        $category=Db::name('union_category')
            ->where('is_visible',1)
            ->order('display_order')
            ->select();
        $this->assign("category",$category);

        $data1=array(
            "module"=>'Union',
            "interface"=>'1001',
            "version"=>'1.0.0',
            "data"=> [
                "page_size"=> $page_size,
                "page"=> $page,
                "category_id"=> $category_id,
                "access_token"=> $access_token
            ]
        );

        if(request()->isAjax())
        {
            $res=curl_post(config('self_url'),json_encode($data1));
            $result_code=json_decode($res,true)['result_code'];
            $result_info=json_decode($res,true)['result_info'];
            if($result_code!=="0000")
            {
                return 0;
            }else{
                $result_data=json_decode($res,true)['result_data']['list'];
                return json_encode($result_data);
            }

        }else{
            $res=curl_post(config('self_url'),json_encode($data1));
            $result_code=json_decode($res,true)['result_code'];
            $result_info=json_decode($res,true)['result_info'];
            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                return $this->error($result_info);

            }
            $result_data=json_decode($res,true)['result_data'];
            $this->assign("list",$result_data['list']);
            $this->assign("cid",$category_id);
            return $this->fetch();
        }

    }

    /*联盟商详情*/
    public function unionInfo(){
        $id=input('id');
        $access_token=Session::get("access_token");
        $data1=array(
            "module"=>'Union',
            "interface"=>'1002',
            "version"=>'1.0.0',
            "data"=> [
                "union_id"=> $id,
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            return $this->error($result_info);

        }else{
            $result_info=json_decode($res,true)['result_data'];
            $this->assign("info",$result_info);
            return $this->fetch();
        }

    }

    /**
     *卡券中心
     */
    public function discountList(){
        $page_size=5;
        $access_token=Session::get("access_token");

        if(request()->isGet()){
            $data=array(
                "module"=>'Union',
                "interface"=>'1003',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "category_id"=>'-1',
                    "page"=>1,
                    "page_size"=>$page_size
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));

            $result_code=json_decode($res,true)['result_code'];

            $result_info=json_decode($res,true)['result_info'];

            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                $this->error($result_info);
            }

            $result_data=json_decode($res,true)['result_data'];
            $this->assign('list',$result_data['list']);
            return $this->fetch();
        }
        if(request()->isAjax()){
            $page = request()->param('page');
            $data=array(
                "module"=>'Union',
                "interface"=>'1003',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "category_id"=>'-1',
                    "page"=>$page,
                    "page_size"=>$page_size
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }
    }

    /**
     *卡券使用说明
     */
    public function discountNote(){
        $access_token=Session::get("access_token");
        $data1=array(
            "module"=>'Union',
            "interface"=>'1004',
            "version"=>'1.0.0',
            "data"=> [
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            return $this->error($result_info);

        }
        $result_data=json_decode($res,true)['result_data'];
        $this->assign("card_explain",$result_data['card_explain']);
        return $this->fetch();

    }
}